<?php
		
	include_once('db/config.php');
		
	class product
	{
		public $code;
		public $name;
		public $unitPrice;
		public $description;
		public $onHand;
		public $category;
		
		public function __contructor()
		{
			$this->code = 0;
			$this->name = '';
			$this->unitPrice = 0;
			$this->description = '';
			$this->onHand = 0;
			$this->category = 0;
		}
		
		public function setProduct($productCode='', $productName='', $unitPrice=0, $description='', $onHand=0,$category=0)
		{
			$this->code = $productCode;
			$this->name = $productName;
			$this->unitPrice = $unitPrice;
			$this->description = $description;;
			$this->onHand = $onHand;
			$this->category = $category;
			
		}
		
		public function retrieveProduct()
		{
			try
			{
				connectDb("everythingdb");
				global $dbLink;
				$resultPointer = mysqli_query($dbLink,"call sp_getProduct(".mysql_real_escape_string($this->code).")");
				if($resultPointer)
				{
					while($resultRow = mysqli_fetch_array($resultPointer))
					{
						$this->code = $resultRow['code'];
						$this->name = $resultRow['name'];
						$this->unitPrice = $resultRow['unit_price'];
						$this->description = $resultRow['description'];
						$this->onHand = $resultRow['on_hand'];
						$this->category = $resultRow['category_code'];
						return $resultRow;
					}
					return false;
				}
				else
				{
					return false;
				}
			}
			catch(Exception $ex)
			{
				throw new Exception($ex->getMessage());
			}
		}
				
		
		public function searchProduct($productCode='', $productName='', $categoryCode='')
		{
			try	
			{
				connectDb("everythingdb");
				global $dbLink;
				$partialQueryString1 = '';
				$partialQueryString2 = '';
				$partialQueryString3 = '';
				$whereClause='';
				if(!empty($categoryCode))
				{
					$partialQueryString1.= " product_category.category_code = ".$categoryCode; 
				}
				if(!empty($productName))
				{
					if(!empty($categoryCode))
					{
						$partialQueryString2.= ' and product.name = "'.$productName.'"';
					}
					else
					{
						$partialQueryString2.= ' product.name = "'.$productName.'"';				
					}
				}
				if(!empty($productCode))
				{
					if(!empty($categoryCode) || !empty($productName))
					{
						$partialQueryString3.= " and product.code = ".$productCode;
					}
					else
					{
						$partialQueryString3.= " product.code = ".$productCode;			
					}
					
				}
				if(!empty($partialQueryString1) || !empty($partialQueryString2) || !empty($partialQueryString3))
				{
					$whereClause= ' where ';
				}				
				
				$queryString = "select product.code,product.name,product.unit_price,product.description,product.on_hand, product_category.category_code, product_image.image_url from product inner join product_category on product.code = product_category.product_code left join product_image on product.code = product_image.product_code ".$whereClause.$partialQueryString1.$partialQueryString2.$partialQueryString3;

				$resultPointer = mysqli_query($dbLink,$queryString);
				if($resultPointer)
				{
					$resultTable = array();
					while($resultRow = mysqli_fetch_assoc($resultPointer))
					{
						array_push($resultTable, $resultRow);
					}
					return $resultTable;
				}
				else
				{
					return false;
				}
			}
			catch(Exception $ex)
			{
				throw new Exception($ex->getMessage());
			}
		}	
		
		public function addProduct()
		{
			try
			{
				connectDb("everythingdb");
				global $dbLink;
				$queryString = "call sp_addProduct('".mysqli_real_escape_string($dbLink,$this->name)."','".mysqli_real_escape_string($dbLink,$this->description)."',".mysqli_real_escape_string($dbLink,$this->unitPrice).",".mysqli_real_escape_string($dbLink,$this->onHand).",".mysqli_real_escape_string($dbLink,$this->category).")";
				//echo $queryString;
				$resultPointer = mysqli_query($dbLink,$queryString);
				//echo $resultPointer;
				if($resultPointer)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			catch(Exception $ex)
			{
				throw new Exception($ex->getMessage());
			}
		}
		
		
		public function deleteProduct()
		{
			try
			{
				connectDb("everythingdb");
				global $dbLink;
				$queryString = "delete from product where code = ".mysql_real_escape_string($this->code);
				//echo $queryString;
				$resultPointer = mysqli_query($dbLink,$queryString);
				//echo $resultPointer;
				if($resultPointer)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			catch(Exception $ex)
			{
				throw new Exception($ex->getMessage());
			}
		}
		
		
		public function updateProduct()
		{
			try
			{
				connectDb("everythingdb");
				global $dbLink;
				
				$queryString = "call sp_updateProduct(".mysqli_real_escape_string($dbLink,$this->code).",'".mysqli_real_escape_string($dbLink,$this->name)."','".mysqli_real_escape_string($dbLink,$this->description)."',".mysqli_real_escape_string($dbLink,$this->unitPrice).",".mysqli_real_escape_string($dbLink,$this->onHand).",".mysqli_real_escape_string($dbLink,$this->category).")";
				$resultPointer = mysqli_query($dbLink,$queryString);

				return true;
			}
			catch(Exception $ex)
			{
				throw new Exception($ex->getMessage());
			}
		}
		
		public function getTopTenProduct()
		{
			try
			{
				connectDb("everythingdb");
				global $dbLink;
				
				$queryString = "SELECT product. * , product_image.image_url FROM product INNER JOIN product_image ON product.code = product_image.product_code LIMIT 10";
				$resultPointer = mysqli_query($dbLink,$queryString);
				$productArray=array();
				
				if($resultPointer)
				{
					while($record = mysqli_fetch_assoc($resultPointer))
					{
						array_push($productArray,$record);
					}
					return $productArray;
				}
				else
				{
					return false;
				}
			}
			catch(Exception $ex)
			{
				throw new Exception($ex->getMessage());
			}
		}
	}
		